<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22
 * Time: 15:49
 */
namespace app\index\controller;
use app\index\model\Order;
use app\index\model\Car;
use app\index\model\Goodsnum;
use app\index\model\Numlog;
use app\index\model\Store;
use think\App;
use think\Request;
use think\facade\Session;
class Jxcorder extends Common{
    public function __construct(App $app = null, Request $request){
        parent::__construct($app, $request);
    }
    //查询订单列表
    public function index(){
        $post=$this->param;
        $limit=10;
        $page=1;
        if (!empty($post['size'])) {
        	$limit=$post['size'];
        }
        if (!empty($post['page'])) {
        	$page=$post['page'];
        }
        $sid=Session::get("shop_id");
        $where=array('sid' => $sid);
        if (!empty($post['sn'])) {
        	$where_sn=array('title' => $post['sn']);
        	$where = array_merge($where,$where_sn);
        }
        if (!empty($post['moblie'])) {
        	$where_m=array('moblie' => $post['moblie']);
        	$where = array_merge($where,$where_m);
        }
        $data['count']=Order::getcount($where,$post['ts'],$post['te']);
        $data['data']=Order::getlist($where,$limit,$limit*($page-1));
        $this->return = $data;
    }
    //取消订单
    public function cancel(){
        $post=$this->param;
        if (empty($post['orderid'])) {
        	$data['code']='1002';
            $data['msg']='订单ID不能为空';
        }else{
        	$where=array('id' => $post['orderid']);
        	$re=Order::cancelorder($where);
        	if ($re) {
        		$data['code']='1001';
            	$data['msg']='';
        	}else{
        		$data['code']='1002';
            	$data['msg']='订单ID有误';
        	}
        }
        $this->return = $data;
    }
    //新建订单
    public function add(){
        $post=$this->param;
        $data['code']='1002';
        $data['msg']='';
        if(empty($post['list'])){
        	$data['code']='1002';
            $data['msg']='同步内容为空';
        }else{
        	foreach ($post['list'] as $key => $value) {
        		$res=Order::getone(array('id' => $value['order_id']));
        		if ($res) {
        			$updates['title']=$value['sn'];
        			$updates['sid']=$value['sid'];
        			$updates['moblie']=$value['moblie'];
        			$updates['integral']=$value['integral'];
        			$updates['uid']=$value['uid'];
        			$updates['username']=$value['username'];
        			$updates['num']=$value['num'];
        			$updates['money']=$value['money'];
        			$updates['paytype']=$value['paytype'];
        			$updates['cancetime']=$value['cancetime'];
        			$updates['remark']=$value['remark'];
        			$updates['status']=$value['status'];
        			$updates['timeline']=$value['timeline'];
        			$updates['moblie']=$value['moblie'];
        			$re=Order::updateorder($updates,array('id' => $value['order_id']));
        		}else{
        			$add_data['id']=$value['order_id'];
        			$add_data['title']=$value['sn'];
        			$add_data['sid']=$value['sid'];
        			$add_data['moblie']=$value['moblie'];
        			$add_data['integral']=$value['integral'];
        			$add_data['uid']=$value['uid'];
        			$add_data['username']=$value['username'];
        			$add_data['num']=$value['num'];
        			$add_data['money']=$value['money'];
        			$add_data['paytype']=$value['paytype'];
        			$add_data['cancetime']=$value['cancetime'];
        			$add_data['remark']=$value['remark'];
        			$add_data['status']=$value['status'];
        			$add_data['timeline']=$value['timeline'];
        			$re=Order::add($add_data);
        		}
        		if ($re) {
    				$data['code']='1001';
    			}
        	}
        }
        $this->return = $data;
    }
    //订单支付成功
    public function finish(){
        $post=$this->param;
        if (empty($post['orderid'])) {
        	$data['code']='1002';
            $data['msg']='订单ID不能为空';
        }else{
        	$where=array('id' => $post['orderid']);
        	$re=Order::finishorder($where);
        	if ($re) {
        		$res=Car::allcar(array('ccid' => $post['orderid']));
        		if ($res) {
        			foreach ($res as $key => $value) {
        				$res1=Goodsnum::getone(array('ccid' => $value['sid'],'gid' => $value['gid']));
        				if ($res1) {
        					if ($res1['num']<$value['num']) {
        						$add_date=array();
								$add_date['cid'] = 14;
								$add_date['gid'] = $value['gid'];
								$add_date['sid'] = $value['sid'];
								$add_date['orderid'] = $post['orderid'];
								$add_date['sn'] = $value['sn'];
								$add_date['stype'] = 1;
								$add_date['num'] = $value['num'];
								$add_date['timeline'] = time();
								Numlog::add($add_date);
								$add_date1=array();
								$add_date1['cid'] = 14;
								$add_date1['gid'] = $value['gid'];
								$add_date1['sid'] = $value['sid'];
								$add_date1['orderid'] = $post['orderid'];
								$add_date1['sn'] = $value['sn'];
								$add_date1['stype'] = 2;
								$add_date1['num'] = $value['num'];
								$add_date1['timeline'] = time();
								Numlog::add($add_date1);
        					}else{
        						$updates['num']=$res1['num']-$value['num'];
				        		$it=Goodsnum::updatenum($updates,array('id' => $res1['id']));
				        		if ($it) {
				        			Store::jsnum(array('id' => $value['sid']),$value['num']);
				        			$add_date=array();
									$add_date['cid'] = 14;
									$add_date['gid'] = $value['gid'];
									$add_date['sid'] = $value['sid'];
									$add_date['orderid'] = $post['orderid'];
									$add_date['sn'] = $value['sn'];
									$add_date['stype'] = 2;
									$add_date['num'] = $value['num'];
									$add_date['timeline'] = time();
									Numlog::add($add_date);
				        		}
        					}
        				}else{
        					$add_date=array();
							$add_date['cid'] = 9;
							$add_date['gid'] = $value['gid'];
							$add_date['ccid'] = $value['sid'];
							$add_date['num'] = 0;
							$add_date['timeline'] = time();
							$res2=Goodsnum::add($add_date);
							if ($res2) {
								$add_date2=array();
								$add_date2['cid'] = 14;
								$add_date2['gid'] = $value['gid'];
								$add_date2['sid'] = $value['sid'];
								$add_date2['orderid'] = $post['orderid'];
								$add_date2['sn'] = $value['sn'];
								$add_date2['stype'] = 1;
								$add_date2['num'] = $value['num'];
								$add_date2['timeline'] = time();
								Numlog::add($add_date2);
								$add_date1=array();
								$add_date1['cid'] = 14;
								$add_date1['gid'] = $value['gid'];
								$add_date1['sid'] = $value['sid'];
								$add_date1['orderid'] = $post['orderid'];
								$add_date1['sn'] = $value['sn'];
								$add_date1['stype'] = 2;
								$add_date1['num'] = $value['num'];
								$add_date1['timeline'] = time();
								Numlog::add($add_date1);
							}
        				}
        			}
        		}
        		$data['code']='1001';
            	$data['msg']='';
        	}else{
        		$data['code']='1002';
            	$data['msg']='订单ID有误';
        	}
        }
        $this->return = $data;
    }
}

